package com.sky.spring.service.impl;


import com.sky.spring.dao.AccountDao;
import com.sky.spring.pojo.Account;
import com.sky.spring.service.TransferService;
import com.sky.spring.utils.ConnectionUtils;
import com.sky.spring.utils.TransactionManager;
import org.springframework.beans.factory.NamedBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Connection;


/**
 * Description:
 * TransferService 实现类
 *
 * @author Sky
 * @Date 2021/7/30 13:04
 */
/*@Component("transferService")*/
//@Service 是 @Component 的别名，为了清晰区分分区。@Service 通常定义在service层
@Service("transferService")
public class TransferServiceImpl implements TransferService {

    // @Autowired 按类型注入。去Spring容器找 AccountDao 的类，然后将其注入进来。
    // 如果一个类有多个bean值的时，会造成无法选择具体注入哪一个的情况。这时需要配合@Qualifier使用
    // @Qualifier 告诉Spring具体装配哪个对象
    @Autowired
    private AccountDao accountDao;


    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

        Account from = accountDao.queryAccountCardNo(fromCardNo);
        Account to = accountDao.queryAccountCardNo(toCardNo);

        from.setMoney(from.getMoney() - money);
        to.setMoney(to.getMoney() + money);

        accountDao.updateAccountByCardNo(from);
        accountDao.updateAccountByCardNo(to);

    }
}
